<template>
  <basic-container>
    <!--<h3>流水页面</h3>-->
    <div style="display: flex; align-items: center; margin-bottom: 20px">
      <span>省：</span>
      <v-distpicker @selected="selected"></v-distpicker>
    </div>
    <avue-crud
      :option="option"
      :data="data"
      :page="page"
      v-model="form"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template slot-scope="scope" slot="menu">
        <el-button type="text" @click="editCompany(scope.row.tenantId)">编辑</el-button>
        <el-button type="text" @click="vip(scope.row)">VIP开通</el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>
<script>
import {
  getmyList,
  editTenant,
  remove,
  update,
  add,
  getNotice
} from "@/api/dept/user";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";
import VDistpicker from "v-distpicker";

export default {
  components: { VDistpicker },
  data() {
    return {
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        tip: false,
        addBtn: false,
        border: true,
        index: false,
        viewBtn: false,
        selection: false,
        columnBtn: false,
        refreshBtn: false,
        searchBtn: false,
        editBtn: false,
        delBtn: false,
        menu: true,
        column: [
          {
            label: "公司ID",
            prop: "id",
            row: true,
            search: true
          },
          {
            label: "公司名称",
            prop: "tenantName",
            row: true,
            search: true
          },
          {
            label: "公司对接人",
            prop: "linkman",
            row: true
          },

          {
            label: "手机",
            prop: "contactNumber",
            row: true
          },
          {
            label: "客户成功经理",
            prop: "managerName",
            row: true
          },
          {
            label: "注册时间",
            prop: "degisterTime",
            row: true
          },
          {
            label: "VIP到期时间",
            prop: "endTime",
            row: true
          },
          {
            label: "使用状态",
            prop: "useStatus",
            search: true,
            type: "radio",
            row: true,
            dicData: [
              {
                value: "0",
                label: "已停用"
              },
              {
                value: "1",
                label: "已注册"
              },
              {
                value: "2",
                label: "正常使用"
              }
            ]
          }
        ]
      },
      data: [],
      dialogVisible: false,
      dialogForm: {
        text: ""
      },
      // 编辑数据
      city: ""
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.notice_add, false),
        viewBtn: this.vaildData(this.permission.notice_view, false),
        delBtn: this.vaildData(this.permission.notice_delete, false),
        editBtn: this.vaildData(this.permission.notice_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },

    vip(item) {
      this.$router.push({
        path: "/vcustom/tenantvip?id=" + item.tenantId
      });
    },
    selected(data) {
      (this.province = data.province.value), (this.city = data.city.value);
    },

    beforeAvatarUpload(file) {
      var testmsg = file.name.substring(file.name.lastIndexOf(".") + 1);
      const extension = testmsg === "xls";
      const extension2 = testmsg === "xlsx";
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!extension && !extension2) {
        this.$message({
          message: "上传文件只能是 xls、xlsx格式!",
          type: "error"
        });
      }
      if (!isLt2M) {
        this.$message({
          message: "上传文件大小不能超过 10MB!",
          type: "error"
        });
      }
      return extension || (extension2 && isLt2M);
    },
    rowSave(row, loading, done) {
      add(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowUpdate(row, index, loading, done) {
      update(row).then(
        () => {
          loading();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        },
        error => {
          done();
          console.log(error);
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    searchReset() {
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    searchChange(params) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleImport() {},
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getNotice(this.form.id).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },

    onLoad(page, params = {}) {
      const param = {
        city: this.city,
        manager: "",
        provice: this.province,
        size: page.pageSize,
        current: page.currentPage,
        id: params.id || "",
        name: params.tenantName || "",
        status: params.useStatus || ""
      };

      getmyList(param).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
      });
    },

    // 编辑公司信息
    editCompany(id) {
      this.$router.push({
        path: "/vcustom/tenantEdit?id=" + id
      });
    }
  }
};
</script>
<style>
.header {
  display: flex;
}
.header .elInput {
  margin-right: 10px;
}
</style>
